package com.sto.stoapp.activity.administrator.hardware;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;

import com.sto.stoapp.activity.R;
import com.sto.stoapp.broadcast.HomeBroadAction;
import com.sto.stoapp.listener.OnBackClickListener;
import com.sto.stoapp.thread.BlueTestThread;
import com.sto.stoapp.widget.BaseDialog;
import com.sto.stoapp.widget.StoHomeActivity;

import sto.android.app.StoPdaKeyEvent;

/**
 * 蓝牙测试
 * 
 * @author mgf
 * 
 */
@SuppressLint("HandlerLeak")
public class AdminBlueTestActivity extends StoHomeActivity {

	private BlueTestThread blueTestThread;

	private Handler blueHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.what == BlueTestThread.BLUE_RESULT) {
				blueTestThread.cancelDialog();
				justResult(getString(msg.arg1));
			}
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.admin_blue_test);
		this.setTitleBottom(R.string.blue_test,
				HomeBroadAction.ADMINISTRATOR_BROAD);
		initWidget();
	}

	private void initWidget() {
		blueTestThread = new BlueTestThread(this,
				BluetoothAdapter.getDefaultAdapter(), blueHandler);
		blueTestThread.showDialogByMessage(getString(R.string.blue_testing));
		blueTestThread.start();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		blueHandler.removeMessages(BlueTestThread.BLUE_RESULT);
	}

	private void justResult(String string) {
		final BaseDialog dialog = new BaseDialog(this);
		dialog.setTitles(string);
		dialog.show();
		dialog.getvCanncel().setVisibility(View.GONE);
		dialog.getvMessage().setVisibility(View.GONE);
		dialog.getvPresentation().setVisibility(View.GONE);
		dialog.setOnBackClickListener(new OnBackClickListener() {

			@Override
			public void onBackClickListener(boolean res) {
				dialog.dismiss();
				finish();
			}
		});
	}
	
    // 添加小退功能
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == StoPdaKeyEvent.KEYCODE_DEL) {
            this.finish();
        }
        return super.onKeyUp(keyCode, event);
    }
}
